﻿using AntdUI;
using Lima.ResumeSys.DAL;
using Lima.ResumeSys.Models;
using Lima.ResumeSys.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace Lima.ResumeSys
{
    public partial class LoginForm : AntdUI.Window
    {
        UserInfoDAL userInfoDAL = new UserInfoDAL();
        public LoginForm()
        {
            InitializeComponent();
            lblError.Text = "";
        }
        
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUsername.Text.Trim())|| string.IsNullOrWhiteSpace(txtPassword.Text.Trim())) {
                lblError.Text = "请输入账号和密码登录！";
                return;
            }
            string username = txtUsername.Text;
            string password = txtPassword.Text;

            string pwd = Sm4Crypto.Encrypt_ECB_Str(password);

            DataRow userInfo = userInfoDAL.GetUser(username, pwd);

            if (userInfo == null)
            {
                MessageBox.Show("用户名或密码错误!", "操作失败", MessageBoxButtons.OK);
                return;
            }
            else
            {
                CurrentUser.UserId = userInfo["UserID"].ToString(); 
                CurrentUser.UserName = username;
                CurrentUser.UserOrgId = userInfo["UserOrg"].ToString();
                CurrentUser.IsLoggedIn = true;
                CurrentUser.IsAdmin=int.Parse( userInfo["IsAdmin"].ToString());
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 如果用户直接点击X关闭登录窗体，退出整个应用
            if (e.CloseReason == CloseReason.UserClosing && this.DialogResult != DialogResult.OK)
            {
                Application.Exit();
            }
        }
    }
}
